package jw.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jw.hospital.common.result.ResultResponse;
import jw.hospital.hosp.model.hospital.HospitalSet;
import jw.hospital.hosp.vo.HospitalSetQueryVo;
import jw.hospital.service.HospitalSetService;
import jw.hospital.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.websocket.server.PathParam;
import java.util.List;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping(value = "/admin/hosp/hospitalset")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    BCryptPasswordEncoder encoder =new  BCryptPasswordEncoder(12);

    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findList")
    public ResultResponse findList() {
        List<HospitalSet> list = hospitalSetService.list();
        return ResultResponse.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("id")
    public ResultResponse removeHosp(@PathParam("id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return ResultResponse.ok();
        } else {
            return ResultResponse.fail();
        }
    }

    @ApiOperation(value = "条件分页查询医院信息")
    @PostMapping("findPageList/{current}/{limit}")
    public ResultResponse findPageList(@PathVariable("current")Long current,
                                       @PathVariable("limit")Long limit,
                                       @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet>  page =new Page<>(current,limit);
        //医院名称
        String hosname = hospitalSetQueryVo.getHosname();
        //医院编号
        String hoscode = hospitalSetQueryVo.getHoscode();
        QueryWrapper wrapper =new QueryWrapper(page);
        if(!StringUtils.isEmpty(hosname)){
            wrapper.eq("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        //调用方法实现分页
        IPage<HospitalSet> list = hospitalSetService.page(page, wrapper);
        //返回结果
        return  ResultResponse.ok(list);
    }

    @ApiOperation(value = "添加医院设置信息")
    @PostMapping(value = "addHospitalSet")
    public  ResultResponse  addHosspitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus("1");
       // String signKey = encoder.encode(hospitalSet.getSignKey());
        hospitalSet.setSignKey(MD5.encrypt( hospitalSet.getSignKey()));
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return  ResultResponse.ok();
        }else {
            return ResultResponse.fail();
        }
    }
    //根据id获取医院设置


    @ApiOperation(value = "修改医院设置信息")
    @PostMapping(value = "updateHospitalSet")
    public ResultResponse updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return  ResultResponse.ok();
        }else {
            return ResultResponse.fail();
        }


    }

    //批量删除
    @ApiOperation(value = "批量删除医院设置信息")
    @DeleteMapping("batchRemove")
    public  ResultResponse  batchHospitalSet(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if(flag){
            return  ResultResponse.ok();
        }else {
            return ResultResponse.fail();
        }

    }

    @ApiOperation(value = "锁定和解锁医院状态")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public ResultResponse lockHospitalSet(@PathVariable("id") Long id,
                                          @PathVariable("status")String status){
        //查询医院
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return ResultResponse.ok();
        }else {
            return ResultResponse.fail();
        }

    }

    @ApiOperation(value="发送签名秘钥")
    @PutMapping("sendSignKey/{id}")
    public ResultResponse  sendSignKey(@PathVariable("id")Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return ResultResponse.ok();
    }








}
